//
//  HTEpisodesScreen.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/24.
//

import SwiftUI

struct HTEpisodesScreen: View {

  var var_id: String = ""
  @EnvironmentObject private var var_store: HTAppStore
  @State private var var_animeInfo: HTAnime! = HTAnime()
  @State private var var_epList: [HTEp]! = []
  @State private var var_currentSsns: [HTEp] = []
  @State private var var_selectedOption: Int = 0

  var body: some View {
    HTNavigationBar{
      VStack(alignment: .leading, spacing: 0){
        Picker("Select", selection: $var_selectedOption) {
          ForEach(0..<(var_animeInfo?.var_ssn_list ?? []).count, id: \.self) { index in
            let var_ssn = (var_animeInfo?.var_ssn_list ?? [])[index]
            Text(var_ssn)
          }
        }
        .pickerStyle(DefaultPickerStyle())
        .labelsHidden()
        .scaledToFit()
        .accentColor(Color.black)
        .background(Color.white)
        .clipShape(Capsule())
        .overlay(
          RoundedRectangle(cornerRadius: 20, style: .continuous)
                   .stroke(Color(hex: "#DCE0EA"), lineWidth: 1)
        )
        .shadow(color: Color.black.opacity(0.04), radius: 5, x: 4, y: 4)
        .onReceive([self.var_selectedOption].publisher.first()) { value in
          if (var_animeInfo.var_eps_dict != nil) {
            var_currentSsns = var_animeInfo.var_eps_dict![var_animeInfo.var_ssn_list![value]]!
          }
        }

        VStack(alignment: .leading, spacing: 0){
          ScrollView(showsIndicators: false){
            VStack(alignment: .leading, spacing: 10){
              ForEach(0..<var_currentSsns.count, id:\.self) { index in
                let var_item = var_currentSsns[index]
                NavigationLink {
                  HTVideoDetailScreen(var_id: var_id, var_epId: var_item.var_id!, var_source: "4")
                } label: {
                  HStack(spacing: 10){
                    ZStack{
                      Image("episode_point")
                        .frame(width: 14, height: 14)
                      Image("episode_play")
                        .frame(width: 10, height: 10)
                    }
                    Text(var_item.var_title ?? "")
                      .font(.system(size: 16))
                      .ht_normal()
                      .lineLimit(1)
                  }
                  .padding(10)
                  .frame(maxWidth: .infinity, alignment: .leading)
                  .background(Color.white)
                  .clipShape(Capsule())
                  .overlay(
                    RoundedRectangle(cornerRadius: 20, style: .continuous)
                             .stroke(Color(hex: "#DCE0EA"), lineWidth: 1)
                  )
                  .shadow(color: Color.black.opacity(0.04), radius: 5, x: 4, y: 4)
                }
              }
            }
          }
          .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
        .padding(.top, 20)
      }
      .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
      .padding(.horizontal, 15)
      .padding(.top, 20)
      .padding(.bottom, UIDevice.ht_safeDistanceBottom())
      .background(Color(hex: "#f5f5f5"))
    }
    .ht_title(title: var_animeInfo.var_title ?? "")
    .onAppear{
      if (var_currentSsns.count == 0) {
        var_store.dispatch(HTEnumAnimeAction.ht_animeInfo(params: ["id": var_id, "uid": "0" ], andCallback: { var_res in
          var_epList = var_res?.var_info?.ht_allEps()
          var_animeInfo = var_res?.var_info
          var_currentSsns = var_animeInfo.var_eps_dict![var_animeInfo.var_ssn_list![var_selectedOption]]!
        }))
      }
    }
  }
}
